package sealedGenerator;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import sealedGenerator.card.Card;
import sealedGenerator.card.Color;
import sealedGenerator.card.Rarity;


public class MWSPatchReader {
	public static Set read(String filename, String name, String expansionSymbol, String packDistribution) {
		Set set = new Set(name, expansionSymbol, packDistribution);
		List<String> properties = new ArrayList<String>();
		
		try {
			File file = new File("patches" + File.separator + filename + ".txt");
			BufferedReader r = new BufferedReader(new FileReader(file));
			
			String line;
			while((line = r.readLine()).trim().length() > 0) {}			
			
			while((line = r.readLine()) != null) {
				if(line.trim().length() > 0)
					properties.add(line);
				else {
					Card card = read(set, properties);
					if (card != null){
						// System.out.println("Adding " + card);
						set.addCard(card  );						
					}
					properties.clear();
				}
			}
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		set.addCard( read(set, properties) );
		
		
		return set;
	}
	
	private static Card read(Set set, List<String> properties) {
		String manaCost = null;
		Integer cmc = 0;
		ArrayList<Color> colors = new ArrayList<Color>();
		String name = null;
		String superType = null;
		String type = null;
		String subType = null;
		String rulesText = null;
		Integer power = null;
		Integer toughness = null;
		Integer cardNum = null;
		Rarity rarity = Rarity.COMMON;
	
		for(String property: properties) {
			String[] parts = property.split(":\t");
			
			String key = "";
			String value = "";
			try {
				key = parts[0].trim();
				value = parts[1].trim();
			}
			catch(Exception e) {
				// System.out.println(property);
			}
			
			if("Card Name".equals(key)) {
				name = value;
			} else if("Card Color".equals(key)) {
				
			} else if("Mana Cost".equals(key)) {
				manaCost = value;
				colors = Color.colorFromCost(value);
			} else if("Type & Class".equals(key)) {
				try {
					parts = value.split(" - ");
					type = parts[0];
					subType = parts[1];

					if(type.contains(" ")) {
						parts = type.split(" ");
						superType = parts[0];
						type = parts[1];
					}
				}
				catch(Exception e) {
					
				}
			} else if("Card Text".equals(key)) {
				rulesText = value;
			} else if("Pow/Tou".equals(key)) {
				try {
					parts = value.split("/");
					power = Integer.parseInt(parts[0]);
					toughness = Integer.parseInt(parts[1]);
				}
				catch(Exception e) {
					
				}
			} else if("Flavor Text".equals(key)) {
				
			} else if("Artist".equals(key)) {
				
			} else if("Rarity".equals(key)) {
				rarity = Rarity.parse(value);
			} else if("Card #".equals(key)) {
				try{
					parts = value.split("/");
					cardNum = Integer.parseInt(parts[0]);
				}catch(NumberFormatException nfe){
					nfe.printStackTrace();
				}
			}
		}
		
		Card card = new Card(manaCost, cmc, colors, name, superType, type, subType, rulesText, power, toughness, rarity, set, cardNum);
		if (name != null){
			//cards without names will not be read. This prevents adding blank cards on invalid input
			return card;
		}
		
		return null;
	}
}
